package com.example.anitamusicspace.controller;

import com.example.anitamusicspace.mapper.UserMapper;
import com.example.anitamusicspace.model.User;
import com.example.anitamusicspace.service.UserService;
import com.example.anitamusicspace.tools.Constant;
import com.example.anitamusicspace.tools.ResponseBodyMessage;
import org.apache.ibatis.binding.BindingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 小梅就是酱子
 * Date: 2022-07-29
 * Time: 20:12
 */
@RestController//@RestController：@ResponseBody ＋ @Controller合在一起的作用。@Controller注解，表明了这个类是一个控制器类 ，@ResponseBody表示方法的返回值直接以指定的格式写入Http response body中
@RequestMapping("/user")//使用 @RequestMapping 来映射请求，也就是通过它来指定控制器可以处理哪些URL请求
public class UserController {
    @Autowired
    private UserService userService;


    //登录
    @Autowired//在自动装配之前，需要完成注入，之后在AppConfig中进行注入
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @RequestMapping("/login")
    public ResponseBodyMessage<User> login(@RequestParam String username, @RequestParam String password, HttpServletRequest request) {
        //关于@RequestParam的解释:将请求参数绑定到你控制器的方法参数上 。如果这个参数是非必传的可以写为@RequestParam(required = false) ，默认是true
        User userInfo=userService.selectByName(username);
        if(userInfo == null){
            return new ResponseBodyMessage<>(-1, "用户名或者密码错误", userInfo);
        }
        else{
            boolean flg=bCryptPasswordEncoder.matches(password,userInfo.getPassword());
            if(!flg){
                return new ResponseBodyMessage<>(-1, "用户名或者密码错误", userInfo);
            }
            request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY,userInfo);//session的创建
            return new ResponseBodyMessage<>(0, "登录成功", userInfo);
        }
    }

    //注册
    @RequestMapping("/register")
    public ResponseBodyMessage<Boolean> register(@RequestParam String username,@RequestParam String password) throws IOException {
        User user1 = userService.selectByName(username);
        if (user1 != null) {
            return new ResponseBodyMessage<>(-1, "当前用户已存在,注册失败", false);
        } else {
            User registerUser = new User();
            registerUser.setUsername(username);
            String registerPassword = bCryptPasswordEncoder.encode(password);
            registerUser.setPassword(registerPassword);
            userService.register(registerUser);
            return new ResponseBodyMessage<>(0, "注册成功", true);
        }
    }

}
